package com.wlyy.bcwlw.sys.audit.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.wlyy.bcwlw.sys.SecurityService;
import com.wlyy.bcwlw.sys.audit.dao.UnprotectUrlDAO;
import com.wlyy.bcwlw.sys.audit.entity.UnprotectUrlDTO;

@Transactional
@Service
public class UnprotectUrlService {

	@Autowired
	private UnprotectUrlDAO unprotectUrlDAO;
	@Autowired
	private SecurityService securityService;
	
	public boolean saveUnProtectUrlInfo(UnprotectUrlDTO unprotectUrlDTO){
		if(unprotectUrlDTO!=null && StringUtils.isEmpty(unprotectUrlDTO.getId())){
			securityService.addUnprotectUrlArray(unprotectUrlDTO.getIsRegexp(), unprotectUrlDTO.getUrl());
			unprotectUrlDAO.createIgnoreExist(unprotectUrlDTO);
		}else{
			UnprotectUrlDTO urlDTO = unprotectUrlDAO.findById(unprotectUrlDTO.getId());
			if(urlDTO == null){
				return false;
			}
			if(!urlDTO.getIsRegexp().equals(unprotectUrlDTO.getIsRegexp()) || !urlDTO.getUrl().equals(unprotectUrlDTO.getUrl())){
				securityService.removeUnprotectUrlArray(urlDTO.getIsRegexp(), urlDTO.getUrl());
				securityService.addUnprotectUrlArray(unprotectUrlDTO.getIsRegexp(), unprotectUrlDTO.getUrl());
			}
			unprotectUrlDAO.update(unprotectUrlDTO);
		}
		return true;
	}
	
	public boolean removeUnProtectUrlById(Integer id){
		if(StringUtils.isEmpty(id)){
			return false;
		}
		UnprotectUrlDTO urlDTO = unprotectUrlDAO.findById(id);
		if(urlDTO == null){
			return false;
		}
		securityService.removeUnprotectUrlArray(urlDTO.getIsRegexp(), urlDTO.getUrl());
		unprotectUrlDAO.removeById(id);
		return true;
	}
}
